package leetcode_400;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *@author 周杨
 *DesignTwitter_355 设计Twitter 并模拟其部分功能
 *describe:用HashMap实现 需要在类中有一个保存userId和他发送的所有twitter的关系的Map，以及userId和他所关注的其他userid的关系的Map。
 *浏览twitter时只要找到用户自己发的twitter，和通过自己所关注的人所发的twitter，并从中选取出10条最近发送的即可。  AC 99%
 *2018年7月9日 上午9:17:28
 */
public class DesignTwitter_355 {
	//封装一条twitter的类
	class Message{
	    int userId;
	    int twitterId;
	    int order;
	    public Message(int userId, int twitterId,int order) {
	        super();
	        this.userId = userId;
	        this.twitterId = twitterId;
	        this.order=order;
	    }
	}
	//对消息进行排序的比较器
	class Compare implements Comparator<Message>{

	    @Override
	    public int compare(Message m1, Message m2) {
	        // TODO Auto-generated method stub
	        if(m1.order>m2.order)
	            return -1;
	        else if(m1.order<m2.order)
	            return 1;
	        else
	            return 0;
	    }
	    
	}
	
	private static int order=0;
    private Map<Integer,Set<Message>> messages;
    private Map<Integer,Set<Integer>> followers;
	 /** Initialize your data structure here. */
    public DesignTwitter_355() {
    	messages = new HashMap<Integer,Set<Message>>();
        followers = new HashMap<Integer,Set<Integer>>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
    	Message m = new Message(userId,tweetId,order++);
        Set<Message> set=messages.getOrDefault(userId, new HashSet<Message>());
        set.add(m);
        messages.put(userId, set);
    }
    
    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    public List<Integer> getNewsFeed(int userId) {
    	List<Message> sets = new ArrayList<Message>();
        //userId发布的消息
        Set<Message> set = messages.getOrDefault(userId, new HashSet<Message>());
        sets.addAll(set);
        //找出他所关注的人发布的消息
        Set<Integer> follow = followers.get(userId);
        if(follow!=null){
            for(int i:follow){
                set=messages.getOrDefault(i, new HashSet<Message>());
                sets.addAll(set);
            }
        }
        //对找出的消息进行排序，并找出最近翻出的10条返回
        List<Integer> result=new ArrayList<Integer>();
        Compare c =new Compare();
        sets.sort(c);
        for(int i=0;i<sets.size()&&i<10;i++){
            Message m=sets.get(i);
            result.add(m.twitterId);
        }
        return result;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
    	if(followeeId==followerId)
            return;
        Set<Integer> set = followers.getOrDefault(followerId, new HashSet<Integer>());
        set.add(followeeId);
        followers.put(followerId, set);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
    	if(followeeId==followerId)
            return;
        if(!followers.containsKey(followerId)){
             ;
         }
         else{
             Set<Integer> set = followers.get(followerId);
             set.remove(followeeId);
             //如果userid的用户已经没有关注的人，讲他的记录从map中删除掉
             if(set.size()==0){
                 followers.remove(followerId);
             }
             else{
             followers.put(followerId, set);
             }
         }
    }

}
